<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 
 * @param 
 *         $xxx = 作用
 * @static 
 *         XXX = 作用
 * 
 * 
 */
$dir = dirname(dirname(__FILE__));
include($dir.'/__queue_global.php');

class ml_mq_update_gd_url extends MqClass{
    const QUEUE_NAME = ML_QUEUENAME_UPDATEGDURL;
    private  $oContent;
    private  $oFeed;
    
    public function __construct($queue)
    {
        $this->oContent = new ml_model_dbContent();
        $this->oFeed=new ml_model_dbUserfeed();
        parent::__construct($queue);
    }
    
    
    public function run_job(){
        //接收的数据
        $arr = $this->src_data;
        $rid=$arr['rid'];
        if(empty($rid)){
            return false;
        }
        
        $rs=$this->oContent->get_by_rid($rid);
        if(!$rs){
            tool_logger::debugLog('UPDATEURL_FALSE' , implode(',',array('rid'=>$rid,'ok'=>'-1')) , true);
            return false;
        }

        $conData=$this->oContent->get_data();
        
var_dump($conData);

        $gd_info=$conData['gd_info'];
        tool_logger::debugLog('DELGOODS_FALSE' , implode(',',array('rid'=>$rid,'ok'=>'0')) , true);
        //检查商品在淘宝是否下架
        $oTaobao=new ml_model_openapiTaobao();
        
        $rs=$oTaobao->getGoodsInfo($gd_info['gd_url']);
        if(!$rs){
            return false;
        }
        $goodsInfo =$oTaobao->get_data();
        var_dump($goodsInfo);
        if(strtotime($goodsInfo['delist_time']) <  time()){
            //删除宝贝gdtmp
            $dbGuangTmp = new ml_model_admin_dbGoodsAudit();
            $dbGuangTmp->delete_goods($rid);
            tool_logger::debugLog('DELGOODS_FALSE' , implode(',',array('rid'=>$rid,'ok'=>'1')) , true);
            return false;
        }
        
        if(isset($gd_info['gd_click_url']) && !empty($gd_info['gd_click_url'])){
            $dbGuangTmp = new ml_model_admin_dbGoodsAudit();
            $dbGuangTmp ->update_gdclick($rid);
            return false;
        }
        
        $shop=ml_tool_shop::url2shop($gd_info['gd_url']);
        switch($shop){
            case 'taobao':
            case 'tmall':
                
                $rs=$oTaobao->trans_taobaourl2taokeurl($gd_info['gd_url']);
                $gd_info['gd_click_url'] =$oTaobao->get_data();
                if(!$rs || empty($gd_info['gd_click_url'])){
                    tool_logger::debugLog('UPDATEURL_FALSE' , implode(',',array('rid'=>$rid,'ok'=>0)) , true);
                    return false;
                }
                tool_logger::debugLog ( 'UPDATEURL_TRUE', implode ( ',', array ('rid' => $rid,'ok' => 1 ) ), true );
                // 生成淘宝客短链
                $oTinyurl = new ml_model_mcTinyurl ();
                $gd_info ['gd_click_tinyurl'] = $oTinyurl->longurl2tiny ( $gd_info ['gd_click_url'] );
                $content = ml_tool_formatContent::repalcegdclick ( $conData ['content'], $gd_info ['gd_tinyurl'], $gd_info ['gd_click_tinyurl'] );
                $rs = $this->oContent->update_content ( $rid, $content );
                if (! $rs) {
                    tool_logger::debugLog ( 'UPDATECONTENTTINYURL_FALSE', implode ( ',', array ('rid' => $rid,'ok' => 1) ), true );
                }
                break;
            default:
                return false;
        }
        
        $rs=$this->oContent->update_gd_info($rid,$gd_info);
        if(!$rs){
            tool_logger::debugLog('UPDATEURL_FALSE' , implode(',',array('rid'=>$rid,'ok'=>2)) , true);
            return false;
        }
        $dbGuangTmp = new ml_model_admin_dbGoodsAudit();
        $dbGuangTmp ->update_gdclick($rid);
        return true;
    }
    

}
ml_run_queue_check(ml_mq_update_gd_url::QUEUE_NAME);

$conf_array = ml_factory::load_standard_conf('redis');
$conf_array = $conf_array['meila_queue'];
$xblog_obj = new ml_mq_update_gd_url(new RsQueue(ml_mq_update_gd_url::QUEUE_NAME , $conf_array));
$argv[1]   = __FILE__;
$xblog_obj->setArgv($argv[1]);
$xblog_obj->execute();
?>